﻿using System;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using netCoreStudy.Entities;
using netCoreStudy.Entities.Base;
using netCoreStudy.IService;

namespace netCoreStudy.Service
{
	public class UserService : BaseService<User>, IUserService
	{
		public UserService(ApiDbContext context, IMapper mapper, ILogger<UserService> logger) : base(
			context, mapper, logger)
		{
		}

		public bool ExistByName(string name)
		{
			return DbSet.Any(x => x.Name == name);
		}

		public async Task<User> GetByNameAsync(string name)
		{
			if (string.IsNullOrWhiteSpace(name)) throw new ArgumentNullException(nameof(name));
			return await GetAll().FirstOrDefaultAsync(x => x.Name == name);
		}
	}
}